<?php

# this script grabs the html files from $rooturl and copy them to local folder, this localfolder should also  be defined at temp.php 

   $rooturl="http://152.20.240.9/RCOOS_html/";
   $localfolder="data/";

           $nos_array=array(8651370,8656483,8652587,8668498,8665530,8664941,8662245,8661070,8659897,8659084,8658120,8658163);
           $nos_array1=array("8651370.htm","8656483.htm","8652587.htm","8668498.htm","8665530.htm","8664941.htm","8662245.htm","8661070.htm","8659897.htm","8659084.htm","8658120.htm","8658163.htm");
           $nos_url="http://tidesandcurrents.noaa.gov/station_info.shtml"; 

           $ndbc_array=array("DUCN7","41025","CLKN7","41013","41004","FBIS1","41002");
           $ndbc_array1=array("DUCN7.htm","41025.htm","CLKN7.htm","41013.htm","41004.htm","FBIS1.htm","41002.htm");
           $ndbc_url="http://www.noaa.gov/station_page.php";

           $nws_array=array("KEDE","KECG","KFFA","KMQI","KHSE","KOCW","KEWN","KNKT","KMRH","KNJM","KNCA","KOAJ","KILM","KSUT","KCRE","KMYR","KCHS","KASJ","KPGV");
           $nws_array1=array("KEDE.htm","KECG.htm","KFFA.htm","KMQI.htm","KHSE.htm","KOCW.htm","KEWN.htm","KNKT.htm","KMRH.htm","KNJM.htm","KNCA.htm","KOAJ.htm","KILM.htm","KSUT.htm","KCRE.htm","KMYR.htm","KCHS.htm","KASJ.htm","KPGV.htm");


            $rcoos_array=array("FRF-17mwaverider","FRF-8mAWAC","FRF-8mAWAC","FRF-JennettePier","FRF-Met","FRF-AlbemarleSound","FRF-8marray","FRP2","ILM2","ILM3","OCP1","LEJ2","LEJ3","CAP1","CAP2","CAP3","FRP1","FRP2","SUN2","SUN3","FollyBeach","Springmaid","NCCOOS-Jennette Pier","NCCOOS-Bogue-Inlet-Pier","NCCOOS-Lookout-shoals-Research-Buoy","nerr-niwol","nerr-acesp","nerr-nocrc");
           $rcoos_array1=array("FRF-17mwaverider","FRF-8mAWAC","FRF-8mAWAC","FRF-JennettePier","FRF-Met","FRF-AlbemarleSound","FRF-8marray","carocoops.FRP2.buoy.htm","cormp.ILM2.buoy.htm","cormp.ILM3.buoy.htm","cormp.OCP1.buoy.htm","cormp.LEJ2.buoy.htm","cormp.LEJ3.buoy.htm","carocoops.CAP1.buoy.htm","carocoops.CAP2.buoy.htm","carocoops.CAP3.buoy.htm","carocoops.FRP1.buoy.htm","carocoops.FRP2.buoy.htm","carocoops.SUN2.buoy.htm","carocoops.SUN3.buoy.htm","FollyBeach","Springmaid","NCCOOS-Jennette Pier","NCCOOS-Bogue-Inlet-Pier","NCCOOS-Lookout-shoals-Buoy","nerr-niwol","nerr-acesp","nerr-nocrc");
          
         $localjs="data/nos.js";  
         $fh = fopen($localjs, 'w') ;
         fwrite($fh, "\n");
         for($i=0;$i<count($nos_array);$i++){
            $remotefile=$nos_array1[$i];
            $remotefile=$rooturl.$remotefile;
            $handle=fopen($remotefile,"r");
            if($handle){

                $content=fread($handle,200000);
                fclose($handle);
                $content=str_replace("\"","'",$content);
                $content=str_replace("\n","",$content);
                if(stristr($content,"error")){$content="Still in development";}
                if($fh){
 
                    fwrite($fh, "data_array['$nos_array[$i]']=\"");
                    fwrite($fh, $content);
                    fwrite($fh,"\";\n\n");
                }
                
            }
         }
         fclose($fh);

         $localjs="data/nws.js";  
         $fh = fopen($localjs, 'w') ;
         for($i=0;$i<count($nws_array);$i++){
            $remotefile=$nws_array1[$i];
            $remotefile=$rooturl.$remotefile;
            $handle=fopen($remotefile,"r");
            if($handle){
                $content=fread($handle,200000);
                fclose($handle);
                $content=str_replace("\"","'",$content);
                $content=str_replace("\n","",$content);
                if(stristr($content,"error")){$content="Still in development";}
                if($fh){
                    fwrite($fh, "data_array['$nws_array[$i]']=\"");
                    fwrite($fh, $content);
                    fwrite($fh,"\";\n\n");
                }
            }
         }        
         fclose($fh);

         $localjs="data/ndbc.js";  
         $fh = fopen($localjs, 'w') ;
        for($i=0;$i<count($ndbc_array);$i++){
            $remotefile=$ndbc_array1[$i];
            $remotefile=$rooturl.$remotefile;
            $handle=fopen($remotefile,"r");
            if($handle){

               $content=fread($handle,200000);
                fclose($handle);
                $content=str_replace("\"","'",$content);
                $content=str_replace("\n","",$content);
                if(stristr($content,"error")){$content="Still in development";}
                if($fh){
                    fwrite($fh, "data_array['$ndbc_array[$i]']=\"");
                    fwrite($fh, $content);
                    fwrite($fh,"\";\n\n");
                }
            }
         }  
         fclose($fh);

         $localjs="data/rcoos.js";  
         $fh = fopen($localjs, 'w') ;
     
        for($i=0;$i<count($rcoos_array);$i++){
            $remotefile=$rcoos_array1[$i];
            $remotefile=$rooturl.$remotefile;
            #$handle=fopen($remotefile,"r");
            #if($handle){
             #  $content=fread($handle,200000);
             #   fclose($handle);
             #   $content=str_replace("\"","'",$content);
             #   $content=str_replace("\n","",$content);
             #   if(stristr($content,"error")){$content="Still in development";}
                $content="Still in Development";
                if($fh){
                    fwrite($fh, "data_array['$rcoos_array[$i]']=\"");
                    fwrite($fh, $content);
                    fwrite($fh,"\";\n\n");
                }
            #}
         }  
        fclose($fh);      

?>
